﻿define([
    'Wjs/ui/widgets/tools/ToolBar',
    'Wjs/ui/widgets/fields/ComboBox'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'TimePicker',

        internal: {

            base: Wjs.getType('ToolBar'),

            initialize: function () {
                var me = this,
                    date = new Date(),
                    hours = date.getHours(),
                    minutes = date.getMinutes(),
                    seconds = date.getSeconds();

                me.addClass('wjs-time-picker');

                me.addItem(

                     Wjs.new('ComboBox')

                            .setName('hour')

                            .setValueField('value')

                            .setTextField('text')

                            .setSelector(

                                Wjs.new('Selector')
                                    .onSelectionChanged(function (cfg) {
                                        var record;

                                        if (cfg.selectionType == 'select') {

                                            record = this.getSelection()[0].getRecord();

                                            if (me.timeSelectedHandler) {

                                                me.timeSelectedHandler();
                                            }
                                        }

                                    })
                            )
                            .setLoader(

                                Wjs.new('LocalLoader')
                                    .setData([
                                        {
                                            value: 1,
                                            text: '01'
                                        },
                                        {
                                            value: 2,
                                            text: '02',
                                        }
                                    ])
                            )
                            .onRender(function () {

                                this.load();
                            })
                );

                this.setTime(hours, minutes, seconds);
            }

        },

        onTimeSelected: function (fcn) {

            this.timeSelectedHandler = Wjs.chain(this.timeSelectedHandler, fcn);

            return this;
        },

        setTime: function (hour, minutes, seconds) {

        },

        selectionChangedHandler: function (cfg) {
            var day;

            if (cfg.selectionType == 'select') {

                day = cfg.currentSelection[0].getContent();

                if (this.timeSelectedHandler) {

                    this.timeSelectedHandler(new Time(this.year, this.month, day));
                }
            }

        }


    });

});